﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI.WebControls;
using NetDon.WebControls;
using NetDon.Utilities;
using SaleDiscount.Portal.eBay;
using eBay.Service.Core.Soap;

namespace SaleDiscount.Portal.Common
{
    public class SaleDiscountPageBase : NetDonPage
    {
        public string EbayUserName
        {
            get
            {
                if (Session["CurrentUser"] == null) return string.Empty;
                return Session["CurrentUser"].ToString();
            }
            set
            {
                
            }
        }

        public string Status
        {
            get
            {
                if (Session["Status"] == null) return "Inactive";
                return Session["Status"].ToString();
            }
            set { Session["Status"] = value; }
        }

        public int? EbayUserTokenId
        {
            get
            {
                if (Session["EbayUserTokenId"] == null) return null;
                return Session["EbayUserTokenId"].ToString().ToInt32();
            }
            set { Session["EbayUserTokenId"] = value; }
        }

        public StoreCustomCategoryTypeCollection StoreCategories
        {
            get
            {
                if (Session["StoreCategories"] == null)
                {
                    StoreCategories = new eBaySession().GeteBayStoreCategories();
                }
                return Session["StoreCategories"] as StoreCustomCategoryTypeCollection;
            }
            set { Session["StoreCategories"] = value; }
        }

        /// <summary>
        /// Verifies the login user.
        /// </summary>
        private void VerifyLoginUser()
        {

            if (string.IsNullOrWhiteSpace(EbayUserName))
            {
                if (!Page.IsCallback)
                {
                    FormsAuthentication.RedirectToLoginPage();
                    Response.End();
                }
            }
          

        }

        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Page.PreInit"/> event at the beginning of page initialization.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
        protected override void OnPreInit(EventArgs e)
        {
            VerifyLoginUser();


            base.OnPreInit(e);
        }


        protected void DateValidator_ServerValidate(object sender, ServerValidateEventArgs e)
        {
            DateTime value;
            e.IsValid = DateTime.TryParse(e.Value, out value);
        }

        protected string GetStatus(object startDate, object endDate)
        {

            if (MiscUtilities.IsNullOrEmpty(startDate) || MiscUtilities.IsNullOrEmpty(endDate)) return "Inactive";

            if (Convert.ToDateTime(startDate.ToString()) > DateTime.Now)
            {
                return "Scheduled";
            }
            if (Convert.ToDateTime(endDate.ToString()) < DateTime.Now)
            {
                return "InActive";
            }
            return "Active";

        }
        
    }
}